const path = require('path');
const webpack = require('webpack');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');

module.exports = {
  entry: {
    waf: './waf.config.js'
  },
  output: {
    path: path.resolve(__dirname, 'dist/compress'),
    filename: '[name].min.js'
  },
  module: {
    rules: [
      {
        test: /\.html$/,
        use: [
          {
            loader: 'html-loader',
            options: {
              minimize: false
            }
          }
        ]
      },
      {
        test: /\.js$/,
        loader: 'babel-loader?compact=false'
      },
      {
        test: /\.(jpg|png|gif|jpeg|bmp)$/,
        use: {
          loader: 'file-loader',
          options: {
            name: '[name].[ext]',
            outputPath: 'images/'
          }
        }
      },
      {
        test: /\.(woff|woff2|svg|ttf|eot)$/,
        use: {
          loader: 'file-loader',
          options: {
            name: '[name].[ext]',
            outputPath: 'fonts/'
          }
        }
      },
      {
        test: /\.scss$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: [
            {
              loader: 'css-loader',
              options: {
                minimize: false,
                sourceMap: false
              }
            },
            {
              loader: 'sass-loader',
              options: {
                minimize: false,
                sourceMap: false
              }
            }
          ]
        })
      }
    ]
  },
  plugins: [
    new webpack.NoEmitOnErrorsPlugin(),
    new webpack.DefinePlugin({ "process.env.NODE_ENV": JSON.stringify("production") }),
    new webpack.LoaderOptionsPlugin({
      minimize: true
    }),
    // new webpack.optimize.CommonsChunkPlugin({
    //   name: 'vendor',
    //   chunks: ['waf']
    // }),
    new CleanWebpackPlugin(['dist/compress']),
    new ExtractTextPlugin({
      filename: './waf.min.css'
    }),
    new UglifyJsPlugin({
      "sourceMap": true,
      "include": /.+\.js/i,
      "uglifyOptions": {
        "warnings": true,
        "compress": {
          "dead_code": false,
          "unused": false,
          "conditionals": false,
          "drop_console": false,
          "drop_debugger": false,
          "keep_fnames": true,
          "typeofs": false
        },
        "mangle": {
        },
        "output": {
          "comments": false
        },
        "toplevel": false
      }
    })
  ],
  devtool: 'source-map'
};
